<!DOCTYPE html>
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>修改商品规格</title>
    <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/layui@2.6.8/dist/css/layui.css">
    <script src="//cdn.jsdelivr.net/npm/layui@2.6.8/dist/layui.js" charset="utf-8"></script>
    <script src="//cdn.jsdelivr.net/npm/wangeditor@latest/dist/wangEditor.min.js" charset="utf-8"></script>
</head>
<body>
    <div style="margin: 15px;padding: 15px;background-color: #fff;">
        <div class="layui-form">
            <input type="hidden" name="id" value="{$row['id']}">
            <div class="layui-form-item">
                <label class="layui-form-label">商品名称</label>
                <div class="layui-input-block">
                    <input type="text" value="{$product['title']}" readonly class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">SKU名称</label>
                <div class="layui-input-block">
                    <input type="text" name="sku_title" value="{$row['sku_title']}" lay-verify="required" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">sku内容</label>
                <div class="layui-input-block">
                    <textarea name="sku_content" lay-verify="required" class="layui-textarea weditor">{$row['sku_content']|raw}</textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">价格</label>
                <div class="layui-input-inline">
                    <input type="text" name="price" value="{$row['price']}" lay-verify="required" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">库存</label>
                <div class="layui-input-inline">
                    <input type="text" name="stock" value="{$row['stock']}" lay-verify="required" class="layui-input">
                </div>
            </div>
            
            <div class="layui-form-item">
                <label class="layui-form-label">状态</label>
                <div class="layui-input-block">
                    <input type="hidden" name="status" value="1">
                    <input type="checkbox" name="status" value="0" {if $row['status'] == 0} checked{/if} lay-skin="switch" lay-text="正常|下架">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="save">保存</button>
                </div>
            </div>
        </div>
    </div>

    <script>
        layui.use(['form'], function(){
            var form = layui.form,
                $ = layui.jquery;

            form.on('submit(save)', function(obj){
                $.post('/admins/products/save_edit_sku', obj.field, function(res) {
                    if (res.code) return layer.alert(res.msg, {icon: 2});
                    layer.alert(res.msg, {icon: 1});
                    setTimeout(() => parent.window.location.reload(), 1000);
                }, 'json');
            });

            // wangeditor富文本
            var weditors = [];
            window.wangEditor && $('.weditor').each(function () {
                var weditor = window.wangEditor;
                var editor_id = $(this).attr('name');
                $(this).attr('id', editor_id).addClass('layui-hide');
                $(this).parent().prepend('<div id="' + editor_id + '"></div>');
                weditors[editor_id] = new weditor(document.getElementById(editor_id));
                weditors[editor_id].config.uploadImgServer = '/admins/files/wangeditor_upload'; //接口url
                weditors[editor_id].config.onchange = function (html) {
                    $('#' + editor_id).parent().find('textarea[name="' + editor_id + '"]').val(html)
                }
                weditors[editor_id].create();
                weditors[editor_id].txt.html($('#' + editor_id).parent().find('textarea[name="' + editor_id + '"]').val())
            });

        });
    </script>
</body>
</html>